<?php

/**
 * CityController is the default controller for this application
 */
class CityController extends ControllerBase {
	
	/**
	 * Admin City Data Service Object
	 * @var cityDataService object
	 */
	private $cityDataService;
	
	/**
	 * CityController Constructor
	 */
	public function __construct() {
		parent::__construct ();
		
		// Set city name
		$this->setPageName ( 'admin_City' );
		
		// Init Authentication class
		$authentication = new Authentication ();
		
		// Only administrator can right
		if (!($authentication->isAdministrator () == true || $authentication->getUserType() == ROLE_DEALMANAGER)) {
			
			// Go to login city
			$url = Zend_Registry::get ( 'config' )->APPLICATION_URL . '/Admin/Login/index';
			CommonUtils::redirect ( $url );
		}
		
		// Get status list of city category
		//$this->smarty->assign ( 'statusList', CityEntity::getStatusList() );
		
		// New city Data Service
		$this->cityDataService = new CityDataService ();
	}
	
	/**
	 * The "index" action is the default action for all controllers -- the
	 * landing city of the site. Return list of all city category
	 *
	 * @access public
	 *
	 * @return void
	 */
	public function indexAction() {
		// View city_category list
		$this->listAction ();
	}
	
	/**
	 * Dispplay List of city_category
	 * landing city of the site. Return list of all city_category
	 *
	 * @access public
	 *
	 * @return void
	 */
	public function listAction() {
		
		$arrElement = array ();
//		$arrElement ['status'] = cityEntity::ACTIVE;
		
		$numRecordPerPage = ITEM_PER_PAGE;
		
		// Pager
		$start = $this->getQuery ( 'start', 0 );
		if ($start == '' || $start < 0) {
			$start = 0;
		}
		
		// Get Totals Templates
		$totals = $this->cityDataService->getTotals ( $arrElement );
		
		$itemList = $this->cityDataService->getItems ( $arrElement, NULL, $start, $numRecordPerPage );
		
		if ($start > $totals) {
			$start = 0;
		}
		$link = Zend_Registry::get ( 'config' )->APPLICATION_URL . "/Admin/City/Index";
		$pageLink = '';
		if ($totals > $numRecordPerPage) {
			$pageUtils = new PagerUtils ( $start, $totals, $numRecordPerPage, 10, $link );
			$pageLink = $pageUtils->writePagesLinks ( 0, 'start' );
			$pageLink .= "<br><span class=\"paing_text_nolink\">" . $pageUtils->writePagesCounter () . "</span>";
		}
		
		$this->smarty->assign ( 'cityEntities', $itemList );
		$this->smarty->assign ( 'this', $this );
		$this->smarty->assign ( 'pageLink', $pageLink );
		
		// create session
		$this->createActionKey ();
		$this->smarty->assign ( 'SMARTY_CONTENT_PATH', 'Admin/City/index.tpl' );
		$this->smarty->display ( APPLICATION_PATH . '/Layouts/Admin/MainLayout.tpl' );
	}
	
	/**
	 * Add action is trigger when admin want to add the city_category information
	 *
	 * @access public
	 *
	 * @return void
	 */
	public function addAction() {
		
		$isError = false;
		$msg = "";
		
		// Get action method and check it
		$actionForm = $this->getPost ( 'submit', '' );
		if (! empty ( $actionForm )) 

		{
			// Get name field
			$city_name = $this->getPost ( 'city_name', "" );
			$city_name = trim ( $city_name );
			// check
			if (! empty ( $city_name )) {
				try {
					
					// Init Authentication class
					$authentication = new Authentication ();
					// Save data into database
					$cityEntity = $this->cityDataService->beforeCreate ();
					$cityEntity->city_name = $city_name;
					$cityId = $this->cityDataService->create ( $cityEntity );
					
					// Redirect to edit city
					$url = Zend_Registry::get ( 'config' )->APPLICATION_URL . '/Admin/City/';
					CommonUtils::redirect ( $url );
					exit ();
				} catch ( Exception $ex ) {
					$isError = true;
					// Ignore to process this case. Only show a message
					$msg = 'The item cannot create successfully. Error Code = ' . $ex->getCode ();
					
					echo $ex->getMessage ();
				
				}
			} else {
				$isError = true;
				
				$msg = 'Please fill in all required fields';
			}
		}
		
		// Create new token key
		$this->createActionKey ();
		$this->smarty->assign ( 'msg', $msg );
		$this->smarty->assign ( 'isError', $isError );
		$this->smarty->assign ( 'city_name', isset ( $city_name ) ? $city_name : "" );
		$this->smarty->assign ( 'SMARTY_CONTENT_PATH', 'Admin/City/add.tpl' );
		$this->smarty->display ( APPLICATION_PATH . '/Layouts/Admin/MainLayout.tpl' );
	}
	
	/**
	 * Edit action is trigger when admin want to edit the city_category information
	 *
	 * @access public
	 *
	 * @return void
	 */
	public function editAction() {
		// Error flag
		$isError = false;
		$msg = "";
		
		// Get id of city
		$id = $this->getPost ( 'id' );
		if (empty ( $id )) {
			$id = $this->getQuery ( 'id' );
		}
		// If the id of city does not exist, go on to view list city
		if (empty ( $id ) || ! is_numeric ( $id )) {
			$this->listAction ();
			return false;
		}
		
		$id = intval ( $id );
			
		// Get data
		$cityEntity = $this->cityDataService->getById ( $id );
		// check data
		if (empty ( $cityEntity )) {
			$this->listAction ();
			return false;
		}
		// Get action method and check it
		$actionForm = $this->getPost ( 'submit', '' );
		
		// submit
		if (! empty ( $actionForm )) {
			// Get city name field
			$city_name = $this->getPost ( 'city_name', "" );
			$city_name = trim ( $city_name );
			
						
			$cityEntity->city_name = $city_name;
			
			// check
			if (! empty ( $city_name )) {
				try {
					$res = $this->cityDataService->update ( $cityEntity );
					$isError = false;
					
					// Show a message
					$msg = 'The record has been updated successfully';
					// Redirect to edit city
					$url = Zend_Registry::get ( 'config' )->APPLICATION_URL . '/Admin/City/';
					CommonUtils::redirect ( $url );
					exit ();
				} catch ( Exception $ex ) {
					
					// Ignore to process this case. Only show a message
					$msg = 'The item cannot create successfully. Error Code = ' . $ex->getCode ();
					$isError = true;
				}
			} else {
				$isError = true;
				
				$msg = 'Please fill in all required fields';
			}
		}
		
		// create session
		$this->createActionKey ();
		$this->smarty->assign ( 'msg', $msg );
		$this->smarty->assign ( 'isError', $isError );
		$this->smarty->assign ( 'city_name', $cityEntity->city_name );
		$this->smarty->assign ( 'id', $cityEntity->id );
		$this->smarty->assign ( 'SMARTY_CONTENT_PATH', 'Admin/City/edit.tpl' );
		$this->smarty->display ( APPLICATION_PATH . '/Layouts/Admin/MainLayout.tpl' );
	}
	
	/**
	 * Delete action is trigger when administrator want to delete a city_category
	 *
	 * @access public
	 *
	 * @return void
	 */
	public function deleteAction() {
		// Get Id of city
		$id = $this->getPost ( 'idValue', 0 );
		
		if ($this->validateActionKey () && ! empty ( $id ) && is_numeric ( $id )) {
			
			$id = intval ( $id );
			// Delete city
			$res = $this->cityDataService->del ( $id );
		}
		$this->listAction ();
	}

}
